package com.sgcc.ln.pmos.e_ln_order.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sgcc.ln.pmos.common.bean.IndentOrContractBean;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.utils.DateUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.common.utils.TextUtil;

/**
 * 合约
 * <p>
 * 下边注释的东西不要删除 因接口不支持 暂时去掉
 */
public class ContractAdapter extends BaseQuickAdapter<IndentOrContractBean, BaseViewHolder> {

    private Context mContext;

    public ContractAdapter(Context context) {
        super(R.layout.item_contract);
        mContext = context;
    }

    public interface OnItemClickListener {
        /**
         * 公司名
         *
         * @param position
         * @param bean
         */
        void OnCompanyNameClickListener(int position, IndentOrContractBean bean);

        /**
         * 查看详情
         *
         * @param position
         * @param bean
         */
        void OnLookinfoClickListener(int position, IndentOrContractBean bean);

        /**
         * 解除合约
         *
         * @param position
         * @param bean
         */
        void OnTerminationClickListener(int position, IndentOrContractBean bean);

        /**
         * 立即评价
         *
         * @param position
         * @param bean
         */
        void OnImmediateEvaluationClickListener(int position, IndentOrContractBean bean);

        /**
         * 再次购买
         *
         * @param position
         * @param bean
         */
        void OnAgainBuyClickListener(int position, IndentOrContractBean bean);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, IndentOrContractBean bean) {

        String img_url = HttpInterConst.BASEURL_IMG + bean.getFileIdComb();
        ImageView imageView = baseViewHolder.getView(R.id.iv_package_logo);
        Glide.with(mContext).load(img_url).placeholder(R.mipmap.order_deft).error(R.mipmap.order_deft).into(imageView);

//        baseViewHolder.setText(R.id.tv_combName, bean.getSellingPoint());
        baseViewHolder.setText(R.id.tv_combName, TextUtil.getName(bean.getSellingPoint(), bean.getCreditRating()));
        baseViewHolder.setText(R.id.tv_company_name, bean.getMembersSdName());

        String start_time = TextUtils.isEmpty(bean.getStartDate()) ? "" : DateUtils.getStrng_to_Times_ymd_2(bean.getStartDate());
        String end_time = TextUtils.isEmpty(bean.getEndDate()) ? "" : DateUtils.getStrng_to_Times_ymd_2(bean.getEndDate());
        baseViewHolder.setText(R.id.tv_time, start_time + "~" + end_time);

        String relStatus = TextUtils.isEmpty(bean.getRelStatus()) ? "" : bean.getRelStatus();
        baseViewHolder.setGone(R.id.tv_mStatus, false);
        switch (relStatus) {
            case "1":
                baseViewHolder.setText(R.id.tv_order_status, "待确认");
                baseViewHolder.setTextColor(R.id.tv_order_status, Color.parseColor("FFFEB004"));
                break;
            case "3":
                baseViewHolder.setText(R.id.tv_order_status, "已生效");
                baseViewHolder.setTextColor(R.id.tv_order_status, Color.parseColor("#2BC98E"));
                break;
            case "4":
                baseViewHolder.setText(R.id.tv_order_status, "已关闭");
                baseViewHolder.setGone(R.id.tv_mStatus, true);
                baseViewHolder.setTextColor(R.id.tv_order_status, Color.parseColor("#666666"));
                break;
            case "5":
                baseViewHolder.setText(R.id.tv_order_status, "已解约");
                baseViewHolder.setTextColor(R.id.tv_order_status, Color.parseColor("#FF2E2E"));
                String startDate = DateUtils.getStrng_to_Times_ymd_3(bean.getStartDate());
                String updateDate = DateUtils.getStrng_to_Times_ymd_3(bean.getUpdateDate());
                if (!TextUtils.isEmpty(startDate)&&!TextUtils.isEmpty(updateDate)) {
                    if(startDate.compareTo(updateDate)>0){
                        //全部解约
                        baseViewHolder.setText(R.id.tv_order_status, "全部解约");
                    }else{
                        //部分解约
                        baseViewHolder.setText(R.id.tv_order_status, "部分解约");
                    }
                }
//                String effectiveDate = DateUtils.getStrng_to_Times_ymd_3(bean.getEffectiveDate());
//                String updateDate = DateUtils.getStrng_to_Times_ymd_3(bean.getUpdateDate());
//                if (!TextUtils.isEmpty(effectiveDate)&&!TextUtils.isEmpty(updateDate)) {
//                    if(effectiveDate.compareTo(updateDate)>0){
//                        //全部解约
//                        baseViewHolder.setText(R.id.tv_order_status, "全部解约");
//                    }else{
//                        //部分解约
//                        baseViewHolder.setText(R.id.tv_order_status, "部分解约");
//                    }
//                }
                break;
            case "6":
                baseViewHolder.setText(R.id.tv_order_status, "已完结");
                baseViewHolder.setTextColor(R.id.tv_order_status, Color.parseColor("#999999"));
                break;
            case "9":
                baseViewHolder.setText(R.id.tv_order_status, "待评价");
                baseViewHolder.setTextColor(R.id.tv_order_status, Color.parseColor("#0092FF"));
                break;
        }


        baseViewHolder.setGone(R.id.tv_ljpj, true);
        baseViewHolder.setGone(R.id.tv_zcgm, true);


//        if ("9".equals(relStatus)) {
//            baseViewHolder.setGone(R.id.tv_ljpj, false);
//        }

        if ("6".equals(relStatus)) {
//            baseViewHolder.setGone(R.id.tv_zcgm, false);
        }


        String result = status_left(bean.getModificationType()) + status_right(bean.getmStatus());
        baseViewHolder.setText(R.id.tv_mStatus, result);


//        baseViewHolder.getView(R.id.tv_jchy).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //待用户确认101或者待售电公司确认102或者（电价变更205&&售电公司已确认104）
//                if ("101".equals(bean.getmStatus()) || "102".equals(bean.getmStatus()) || (
//                        "104".equals(bean.getmStatus()) && "205".equals(bean.getModificationType()))) {
//                    ToastUtils.showShort("当前合约有流程未结束，暂不能解约");
//                } else {
//                    if (mOnItemClickListener != null) {
//                        mOnItemClickListener.OnTerminationClickListener(getItemPosition(bean), bean);
//                    }
//                }
//            }
//        });

        baseViewHolder.getView(R.id.rl_content).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnLookinfoClickListener(getItemPosition(bean), bean);
                }
            }
        });
        baseViewHolder.getView(R.id.tv_company_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnCompanyNameClickListener(getItemPosition(bean), bean);
                }
            }
        });
        baseViewHolder.getView(R.id.tv_ljpj).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnImmediateEvaluationClickListener(getItemPosition(bean), bean);
                }
            }
        });
        baseViewHolder.getView(R.id.tv_zcgm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnAgainBuyClickListener(getItemPosition(bean), bean);
                }
            }
        });


//        if (getItemPosition(s) == 0) {
//            onChangLayout(baseViewHolder, false);
//            baseViewHolder.getView(R.id.iv_arrow).setRotation(180);
//        } else {
//            onChangLayout(baseViewHolder, true);
//        }
//
//        RecyclerView recyclerView = baseViewHolder.getView(R.id.rv_month);
//        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
//        recyclerView.setLayoutManager(mLayoutManager);
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
//        ContractMonthAdapter contractMonthAdapter = new ContractMonthAdapter();
//        recyclerView.setAdapter(contractMonthAdapter);
//        List<String> list = new ArrayList<>();
//        for (int i = 0; i < 2; i++) {
//            list.add(i + "");
//        }
//        contractMonthAdapter.setNewData(list);
//
//
//        baseViewHolder.getView(R.id.rl_content).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (mOnItemClickListener != null) {
//                    mOnItemClickListener.OnItemsClickListener(getItemPosition(s), s);
//                }
//            }
//        });
//
//        baseViewHolder.getView(R.id.iv_arrow).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                baseViewHolder.getView(R.id.iv_arrow).setRotation(180);
//            }
//        });
    }

//    public void onChangLayout(BaseViewHolder baseViewHolder, boolean changed) {
//        if (changed) {
//            baseViewHolder.setGone(R.id.rv_month, true);
//        } else {
//            baseViewHolder.setGone(R.id.rv_month, false);
//        }
//    }


    public String status_left(String modificationType) {
        String result = "";
        switch (modificationType) {
            case "201":
                result = "合约建立";
                break;
            case "202":
                result = "电量变更";
                break;
            case "203":
                result = "议价";
                break;
            case "204":
                result = "合约解除";
                break;
            case "205":
                result = "电价变更";
                break;
            case "206":
                result = "备案补充协议";
                break;
        }
        return result;
    }

    public String status_right(String mStatus) {
        String result = "";
        switch (mStatus) {
            case "101":
                result = "待用户确认";
                break;
            case "102":
                result = "待售电公司确认";
                break;
            case "103":
                result = "用户已确认";
                break;
            case "104":
                result = "售电公司已确认";
                break;
            case "105":
                result = "用户已驳回";
                break;
            case "106":
                result = "售电公司已驳回";
                break;
            case "107":
                result = "用户已撤回";
                break;
            case "108":
                result = "售电公司已撤回";
                break;
        }
        return result;
    }
}
